package com.netty.chat.service;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.honour.service.interfaced.UserInManager;
import com.netty.common.SessionManager;
import com.netty.common.module.ChatCmd;
import com.netty.common.module.ChatType;
import com.netty.common.module.ModuleId;
import com.system.util.PageData;
/**
 * @Function 聊天服务
 * @Author zhaozhenhua
 * @Date 2018年5月28日
 */
@Service("chatService")
public class ChatServiceImpl implements ChatService{
	
	
	@Resource(name = "userinService")
	private UserInManager userinService;

	@Override
	public void publicChat(String HONOURUSER_ID, String CONTENT) {
		try {
			//Player player = playerDao.getPlayerById(playerId);
			PageData pd = new PageData();
			pd.put("HONOURUSER_ID", HONOURUSER_ID);
			PageData user = userinService.getPortNickName(pd);
			//获取所有在线玩家
			Set<String> onlinePlayers = SessionManager.getOnlinePlayers();
			
			//创建消息对象
//			ChatResponse chatResponse = new ChatResponse();
//			chatResponse.setChatType(ChatType.PUBLIC_CHAT);
//			chatResponse.setSendPlayerId(player.getPlayerId());
//			chatResponse.setSendPlayerName(player.getPlayerName());
//			chatResponse.setMessage(content);
			PageData chatResponse = new PageData();
			chatResponse.put("CHATTYPE", ChatType.PUBLIC_CHAT);
			chatResponse.put("HONOURUSER_ID", user.get("HONOURUSER_ID"));
			chatResponse.put("NICKNAME", user.get("NICKNAME"));
			chatResponse.put("CONTENT", CONTENT);
			
			//发送消息
			for(String targetPlayerId : onlinePlayers){
				SessionManager.sendMessage(targetPlayerId, ModuleId.CHAT, ChatCmd.PUSHCHAT, chatResponse);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public void privateChat(String HONOURUSER_ID, String TARGETHONOURUSER_ID, String CONTENT) {
		//不能和自己私聊
		if(HONOURUSER_ID == TARGETHONOURUSER_ID){
			System.out.println("不能和自己私聊");
			//throw new ErrorCodeException(ResultCode.CAN_CHAT_YOUSELF);
		}
		
		PageData pd = new PageData();
		pd.put("HONOURUSER_ID", HONOURUSER_ID);
		PageData user;
		try {
			user = userinService.getPortNickName(pd);
			//判断目标是否存在
			pd.put("HONOURUSER_ID", TARGETHONOURUSER_ID);
			PageData targetUser = userinService.getPortNickName(pd);
			if(targetUser == null){
				System.out.println("目标用户不存在");
				//throw new ErrorCodeException(ResultCode.PLAYER_NO_EXIST);
			}
			
			//判断对方是否在线
			if(!SessionManager.isOnlinePlayer(TARGETHONOURUSER_ID)){
				System.out.println("对方用户不在线");
				//throw new ErrorCodeException(ResultCode.PLAYER_NO_ONLINE);
			}
			
			//创建消息对象
			PageData chatResponse = new PageData();
			chatResponse.put("ChatType", ChatType.PRIVATE_CHAT);
			chatResponse.put("HONOURUSER_ID", user.get("HONOURUSER_ID"));
			chatResponse.put("NICKNAME", user.get("NICKNAME"));
			chatResponse.put("TARGETHONOURUSER_ID", targetUser.get("HONOURUSER_ID"));
			chatResponse.put("CONTENT", CONTENT);
			
			//给目标对象发送消息
			SessionManager.sendMessage(TARGETHONOURUSER_ID, ModuleId.CHAT, ChatCmd.PUSHCHAT, chatResponse);
			//给自己也回一个(偷懒做法)
			SessionManager.sendMessage(HONOURUSER_ID, ModuleId.CHAT, ChatCmd.PUSHCHAT, chatResponse);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
}
